package userInterface;
import dataProces.DatabaseOperations;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class signUp extends JDialog implements ActionListener {
    JPanel[] jPanels = new JPanel[3];
    JButton[] jButtons = new JButton[2];
    JTextField[] jFields = new JTextField[4];
    String[] prompt = { "身份证号", "姓名", "设置密码", "电话", "注册", "返回", "注册界面" };

    public signUp() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3, 1));
        for (int i = 0; i < jPanels.length; i++) {
            jPanels[i] = new JPanel();
            if (i < 2)
                for (int j = i * 2; j < (i + 1) * 2; j++) {
                    jPanels[i].add(new JLabel(prompt[j]));
                    jFields[j] = new JTextField(15);
                    jPanels[i].add(jFields[j]);
                }
            else
                for (int j = 0; j < 2; j++) {
                    jButtons[j] = new JButton(prompt[2 * i + j]);
                    jButtons[j].addActionListener(this);
                    jPanels[i].add(jButtons[j]);
                }
            container.add(jPanels[i]);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[6]);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtons[0]) {
            DatabaseOperations dOperations = new DatabaseOperations();
            String[] userData = new String[5];
            String[] investmentData = new String[6];
            int account = (int) (Math.random() * 100000);
            for (int i = 0; i < 5; i++) {
                if (i == 0) {
                    while (dOperations.dataQueries("user",Integer.toString(account),"account") != null)
                        account = (int) (Math.random() * 100000);
                    userData[i] = String.format("%06d",account);
                } else
                    userData[i] = jFields[i - 1].getText();
            }
            for (int i = 0; i < 6; i++) {
                if (i == 0)
                    investmentData[i] = String.format("%06d",account);
                else
                    investmentData[i] = "0";
            }
            if (dOperations.dataInsert(userData, investmentData))
                JOptionPane.showMessageDialog(null, "注册成功！\n已为您生成账号:"+String.format("%06d",account));
            dispose();
            new Login();
        } else if (e.getSource() == jButtons[1]) {
            dispose();
            new Login();
        }
    }

    public static void main(String[] args) {
        new signUp();
    }
}
